package com.gengzp.mq.server.controller;

import com.gengzp.mq.common.model.base.MqPaginationResult;
import com.gengzp.mq.common.model.base.MqResult;
import com.gengzp.mq.common.model.req.PageGengzpMqMessageReq;
import com.gengzp.mq.common.model.resp.GengzpMqMessageExt;
import com.gengzp.mq.common.model.resp.PageGengzpMqMessageResp;
import com.gengzp.mq.common.utils.GengzpMqAssertUtils;
import com.gengzp.mq.server.service.IGengzpMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName GengzpMqController
 * @Description 消息队列框架控制层
 * @Author gengzp
 * @Date 2025/6/8 13:52
 */
@RestController
@RequestMapping("/gengzp/mq")
public class GengzpMqController {

    @Autowired
    private IGengzpMqService gengzpMqService;

    /**
     * 发送消息
     *
     * @param topic          消息主题
     * @param messageContent 消息内容
     * @return 消息id
     */
    @PostMapping("/send")
    public MqResult<Long> sendMessage(@RequestParam String topic,
                                      @RequestParam String messageContent) {
        GengzpMqAssertUtils.isNotBlank(topic, "topic不能为空");
        GengzpMqAssertUtils.isNotBlank(messageContent, "messageContent不能为空");
        return MqResult.getSuccessResult(gengzpMqService.sendMessage(topic, messageContent));
    }

    /**
     * 订阅消息
     *
     * @param consumerServiceIp   消费者ip地址
     * @param consumerServiceName 消费者服务名
     * @param topic               消息主题
     * @param consumerGroup       消费者组
     * @return 消息
     */
    @PostMapping("/receive")
    public MqResult<GengzpMqMessageExt> receiveMessage(@RequestParam String consumerServiceIp,
                                                       @RequestParam String consumerServiceName,
                                                       @RequestParam String topic,
                                                       @RequestParam String consumerGroup) {
        GengzpMqAssertUtils.isNotBlank(consumerServiceIp, "consumerServiceIp不能为空");
        GengzpMqAssertUtils.isNotBlank(consumerServiceName, "consumerServiceName不能为空");
        GengzpMqAssertUtils.isNotBlank(consumerGroup, "consumerGroup不能为空");
        GengzpMqAssertUtils.isNotBlank(topic, "topic不能为空");
        return MqResult.getSuccessResult(gengzpMqService.receiveMessage(consumerServiceIp, consumerServiceName, topic, consumerGroup));
    }

    /**
     * 确认消息
     *
     * @param messageId 消息id
     */
    @PostMapping("/confirm")
    public MqResult<?> confirmMessage(@RequestParam Long messageId) {
        GengzpMqAssertUtils.nonNull(messageId, "messageId不能为空");
        gengzpMqService.confirmMessage(messageId);
        return MqResult.getSuccessResult();
    }

    /**
     * 分页查询消息
     *
     * @param req 分页查询请求实体
     * @return 分页查询结果
     */
    @PostMapping("/page")
    public MqResult<MqPaginationResult<PageGengzpMqMessageResp>> pageQueryMessage(@RequestBody PageGengzpMqMessageReq req) {
        return MqResult.getSuccessResult(gengzpMqService.pageQueryMessage(req));
    }

    /**
     * 分页查询死信队列消息 (消费失败达到3次)
     *
     * @param req 分页查询请求实体
     * @return 分页查询结果
     */
    @PostMapping("/page_death")
    public MqResult<MqPaginationResult<PageGengzpMqMessageResp>> pageQueryDeathMessage(@RequestBody PageGengzpMqMessageReq req) {
        return MqResult.getSuccessResult(gengzpMqService.pageQueryDeathMessage(req));
    }

}
